import dayjs from "dayjs";
import Big from "big.js";
import { TableResultObject } from "./interface";
import weekday from "dayjs/plugin/weekday";
dayjs.extend(weekday); //  use

// 处理接口返回数据
export function resolveSearchData(data: any) {
  let result: any = [];
  if (data?.length) {
    data.map((item: any) => {
      const elderItem = {
        ...item,
        // key: !item.id ? generateUniqueKey() : "",
      };
      const oneLine: any = [elderItem, elderItem, elderItem];
      result = [...result, ...oneLine];
    });
  }

  return result;
}

// 前端唯一值计算
export function generateUniqueKey() {
  const key = "key_";
  const time = Date.now().toString(36);
  const random = Math.random().toString(36).substr(2, 9);
  return key + time + random;
}

// 单元格合并 param表格数据  rowCount合并行数  columnCount合并前多少行
export const objectSpanMethod = (
  param: any,
  rowCount: number,
  columnCount: number
) => {
  if (param.columnIndex < columnCount) {
    if (param.rowIndex % rowCount === 0) {
      return {
        rowspan: rowCount,
        colspan: 1,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};

// 获取从startDate开始，向后推intervalDay的所有天数
export function getDays(startDate: any, intervalDay: any) {
  const newIntervalDay = intervalDay + 1;
  // 创建起始日期的Dayjs对象
  const dayjsStartDate = dayjs(startDate);

  // 获取下一个月的第一天
  const nextMonthFirstDay = dayjsStartDate
    .startOf("day")
    .add(newIntervalDay, "day");

  // 使用一个循环来获取所有日期
  let currentDate = dayjsStartDate;
  const dates = [];

  while (currentDate.isBefore(nextMonthFirstDay)) {
    const row:any = {};
    row.date = currentDate.format("MM/DD");
    row.week = currentDate.weekday();
    dates.push(row);
    // dates.push(currentDate.format("MM/DD"));
    currentDate = currentDate.add(1, "day");
  }
  return dates;
}

const weeksCn = [
  "星期日",
  "星期一",
  "星期二",
  "星期三",
  "星期四",
  "星期五",
  "星期六",
];

// 获取这个月的所有天数
export function getDaysByMonth(currentMonth: any) {
  // 获取当前月份的第一天
  const startOfMonth = dayjs(currentMonth).startOf("month");

  // 获取当前月份的最后一天
  const endOfMonth = dayjs(currentMonth).endOf("month");

  // 循环获取每一天
  let currentDay = startOfMonth;
  const daysOfMonth = [];

  while (currentDay.isBefore(endOfMonth)) {
    daysOfMonth.push({
      date: currentDay.format("MM/DD"),
      week: weeksCn[currentDay.weekday()],
    }); // 将日期格式化为 'YYYY-MM-DD' 字符串
    currentDay = currentDay.add(1, "day"); // 增加一天
  }

  console.log(daysOfMonth); // 打印当前月份的所有天
  return daysOfMonth;
}

// 头部样式处理函数
export function leftHeaderStyle({ columnIndex }: any) {
  if (columnIndex === 0) {
    return "seltAllbtnDis";
  }
}

// el-table checkbox是否可选中处理函数
export function selectableStatus(row: TableResultObject) {
  // 如果行数据中有disabled属性，则返回false禁用多选
  if (row.id == "-999") {
    return false;
  } else {
    return !row.disabled;
  }
}

// 颜色处理方法
export function inConsistent(
  row: TableResultObject,
  $index: number,
  dayIndex: number
) {
  let className = "";
  // 第一行颜色
  if (($index + 1) % 3 == 1) {
    row.listPlanActual.forEach((item) => {
      if (getMonthAndDay(item.requireDate) === day.date) {
        detailData.id = item.id;
        detailData.planQuantity = item.planQuantity;
        // detailData = item
        flag = false;
      }
    });

    if (row.details[dayIndex]?.status == 1) {
      className = "#ff0000";
    } else if (row.details[dayIndex]?.status == 1) {
      className = "#FFF";
    }
    // else if (row.details[dayIndex]?.status == 2) {
    //   className = "#FEF1F1";
    // } else if (row.details[dayIndex]?.colorType == 3) {
    //   className = "#FFFDBD";
    // } else if (
    //   row.details[dayIndex]?.colorType == 4 &&
    //   row.details[dayIndex]?.demandStatus != 3
    // ) {
    //   className = "#55EFC4";
    // } else if (row.details[dayIndex]?.colorType == 5) {
    //   className = "#FFD700";
    // }
  }
  // 第二行颜色
  if (($index + 1) % 3 == 2) {
    if (row.listPlanActual[dayIndex]?.colorType == 0) {
      className = "";
    } else if (row.listPlanActual[dayIndex]?.colorType == 1) {
      className = "#95D475";
    } else if (row.listPlanActual[dayIndex]?.colorType == 2) {
      className = "#FEF1F1";
    } else if (row.listPlanActual[dayIndex]?.colorType == 3) {
      className = "#FFFDBD";
    } else if (row.listPlanActual[dayIndex]?.colorType == 4) {
      className = "#55EFC4";
    } else if (row.listPlanActual[dayIndex]?.colorType == 5) {
      className = "#FFD700";
    }
  }
  // 第三行颜色
  if (($index + 1) % 3 == 0 && row.details[dayIndex]?.demandStatus == 3) {
    if (row.details[dayIndex]?.colorType == 0) {
      className = "";
    } else if (row.details[dayIndex]?.colorType == 1) {
      className = "#95D475";
    } else if (row.details[dayIndex]?.colorType == 2) {
      className = "#FEF1F1";
    } else if (row.details[dayIndex]?.colorType == 3) {
      className = "#FFFDBD";
    } else if (row.details[dayIndex]?.colorType == 4) {
      className = "#55EFC4";
    } else if (row.details[dayIndex]?.colorType == 5) {
      className = "#FFD700";
    }
  }
  return className;
}
function getMonthAndDay(date: any) {
  const month = date.slice(5, 7);
  const day = date.slice(8, 10);
  return month + "/" + day;
}
// 颜色处理方法(返回数据不是所有数据)
export function inConsistentOne(row: any, $index: number, detailIndex: number) {
  let className = "";
  // 第一行颜色
  if (($index + 1) % 3 == 1) {
    if (row.details[detailIndex]?.status == 1) {
      className = "#FEF1F1";
    }
  }
  return className;
}
